/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.railway.reports;

import com.railway.reports.utils.QueryBuilder;
import com.railway.utils.CustomScrollBarUI;
import com.railway.utils.RestServer;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.swing.DefaultListModel;
import javax.swing.JSpinner;
import javax.swing.SpinnerDateModel;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 *
 * @author Kenny
 */
public class ScheduleCustomizer extends javax.swing.JPanel {

    private QueryBuilder qb;
    private SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
    private DefaultListModel<String> dlmRoute;
    private DefaultListModel<String> dlmTrain;
    private Calendar c = Calendar.getInstance();
    private Date initDate;
    private Date maxDate;
    private boolean depTime = false;
    private boolean route = false;
    private boolean train = false;
    private int initialHave = 0;

    /**
     * Creates new form ScheduleCustomizer
     */
    public ScheduleCustomizer() {
        c.set(1970, 0, 1, 5, 0, 0);
        c.set(Calendar.MILLISECOND, 0);
        initDate = c.getTime();
        c.set(1970, 0, 1, 23, 0, 0);
        c.set(Calendar.MILLISECOND, 0);
        maxDate = c.getTime();
        initComponents();
        populateRouteList();
        populateTrainList();
    }

    private void populateRouteList() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                dlmRoute = new DefaultListModel<>();
                String result = RestServer.httpGet("route", "");
                try {
                    JSONObject json = new JSONObject(result);
                    boolean error = json.optBoolean("error");
                    if (!error) {
                        int statuscode = json.getInt("status");
                        if (statuscode == 200) {
                            JSONArray jsonArr = json.getJSONArray("content");
                            for (int i = 0; i < jsonArr.length(); i++) {
                                dlmRoute.addElement(jsonArr.getJSONObject(i).getString("routeName"));
                            }
                            jltRoute.setModel(dlmRoute);
                        }
                    }
                } catch (JSONException ex) {
                }
            }
        }).start();
    }

    private void populateTrainList() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                dlmTrain = new DefaultListModel<>();
                String result = RestServer.httpGet("train", "");
                try {
                    JSONObject json = new JSONObject(result);
                    boolean error = json.optBoolean("error");
                    if (!error) {
                        int statuscode = json.getInt("status");
                        if (statuscode == 200) {
                            JSONArray jsonArr = json.getJSONArray("content");
                            for (int i = 0; i < jsonArr.length(); i++) {
                                dlmTrain.addElement(jsonArr.getJSONObject(i).getString("trainName"));
                            }
                            jltTrain.setModel(dlmTrain);
                        }
                    }
                } catch (JSONException ex) {
                }
            }
        }).start();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jbgTimeGroup = new javax.swing.ButtonGroup();
        jbgReportType = new javax.swing.ButtonGroup();
        jbgExceptionType = new javax.swing.ButtonGroup();
        jbgPurchaseTime = new javax.swing.ButtonGroup();
        jspCustomizer = new javax.swing.JScrollPane();
        jpCustomizer = new javax.swing.JPanel();
        jlblScheduleCustomizer = new javax.swing.JLabel();
        jsprCustomizer = new javax.swing.JSeparator();
        jrbSumReport = new javax.swing.JRadioButton();
        jcbDepTime = new javax.swing.JCheckBox();
        jrbTimeGreater = new javax.swing.JRadioButton();
        jspiGreaterTime = new javax.swing.JSpinner(
            new SpinnerDateModel(initDate, initDate, maxDate, Calendar.HOUR)
        );
        JSpinner.DateEditor timeEditor = new JSpinner.DateEditor(jspiGreaterTime, "hh:mm a");
        timeEditor.getTextField().setEditable(false);
        jspiGreaterTime.setEditor(timeEditor);
        jrbTimeLess = new javax.swing.JRadioButton();
        jspiLessTime = new javax.swing.JSpinner(
            new SpinnerDateModel(initDate, initDate, maxDate, Calendar.HOUR)
        );
        JSpinner.DateEditor timeEditor2 = new JSpinner.DateEditor(jspiLessTime, "hh:mm a");
        timeEditor2.getTextField().setEditable(false);
        jspiLessTime.setEditor(timeEditor2);
        jcbRoute = new javax.swing.JCheckBox();
        jspRoute = new javax.swing.JScrollPane();
        jltRoute = new javax.swing.JList();
        jrbTrain = new javax.swing.JCheckBox();
        jspTrain = new javax.swing.JScrollPane();
        jltTrain = new javax.swing.JList();
        jrbExpReport = new javax.swing.JRadioButton();
        jrbMostPopular = new javax.swing.JRadioButton();
        jlblMostTop = new javax.swing.JLabel();
        jspiMostTop = new javax.swing.JSpinner();
        jrbLessPopular = new javax.swing.JRadioButton();
        jspiLessTop = new javax.swing.JSpinner();
        jlblLessTop = new javax.swing.JLabel();
        jrbPruchaseTimes = new javax.swing.JRadioButton();
        jrbTimesMoreThan = new javax.swing.JRadioButton();
        jrbTimesLessThan = new javax.swing.JRadioButton();
        jspiTimesMoreThan = new javax.swing.JSpinner();
        jspiTimesLessThan = new javax.swing.JSpinner();
        jrbTimesMoreEquals = new javax.swing.JRadioButton();
        jrbTimesLessEquals = new javax.swing.JRadioButton();
        jrbTimesEquals = new javax.swing.JRadioButton();
        jspiTimesMoreEquals = new javax.swing.JSpinner();
        jspiTimesEquals = new javax.swing.JSpinner();
        jspiTimesLessEquals = new javax.swing.JSpinner();
        jrbTimesNotEquals = new javax.swing.JRadioButton();
        jspiTimesNotEquals = new javax.swing.JSpinner();

        jspCustomizer.setBackground(new java.awt.Color(255, 255, 255));
        jspCustomizer.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(220, 220, 220), 2));
        jspCustomizer.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        jspCustomizer.getVerticalScrollBar().setUI(new CustomScrollBarUI());
        jspCustomizer.getVerticalScrollBar().setUnitIncrement(16);

        jpCustomizer.setBackground(new java.awt.Color(255, 255, 255));
        jpCustomizer.setPreferredSize(new java.awt.Dimension(190, 670));
        jpCustomizer.setLayout(null);

        jlblScheduleCustomizer.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jlblScheduleCustomizer.setText("CRITERIA CUSTOMIZER");
        jpCustomizer.add(jlblScheduleCustomizer);
        jlblScheduleCustomizer.setBounds(10, 5, 200, 20);
        jpCustomizer.add(jsprCustomizer);
        jsprCustomizer.setBounds(5, 25, 200, 5);

        jrbSumReport.setBackground(new java.awt.Color(255, 255, 255));
        jbgReportType.add(jrbSumReport);
        jrbSumReport.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jrbSumReport.setSelected(true);
        jrbSumReport.setText("Summary/Details Report");
        jrbSumReport.setActionCommand("sum");
        jrbSumReport.setFocusPainted(false);
        jrbSumReport.setOpaque(false);
        jrbSumReport.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jrbSumReportActionPerformed(evt);
            }
        });
        jpCustomizer.add(jrbSumReport);
        jrbSumReport.setBounds(10, 30, 180, 20);

        jcbDepTime.setBackground(new java.awt.Color(255, 255, 255));
        jcbDepTime.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jcbDepTime.setText("Departure Time");
        jcbDepTime.setFocusPainted(false);
        jcbDepTime.setOpaque(false);
        jcbDepTime.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jcbDepTimeActionPerformed(evt);
            }
        });
        jpCustomizer.add(jcbDepTime);
        jcbDepTime.setBounds(20, 50, 170, 20);

        jrbTimeGreater.setBackground(new java.awt.Color(255, 255, 255));
        jbgTimeGroup.add(jrbTimeGreater);
        jrbTimeGreater.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jrbTimeGreater.setSelected(true);
        jrbTimeGreater.setText("Greater or equal to");
        jrbTimeGreater.setActionCommand(">=");
        jrbTimeGreater.setEnabled(false);
        jrbTimeGreater.setFocusPainted(false);
        jrbTimeGreater.setOpaque(false);
        jrbTimeGreater.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jrbTimeGreaterActionPerformed(evt);
            }
        });
        jpCustomizer.add(jrbTimeGreater);
        jrbTimeGreater.setBounds(30, 70, 150, 20);

        jspiGreaterTime.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jspiGreaterTime.setEnabled(false);
        jpCustomizer.add(jspiGreaterTime);
        jspiGreaterTime.setBounds(50, 90, 130, 20);

        jrbTimeLess.setBackground(new java.awt.Color(255, 255, 255));
        jbgTimeGroup.add(jrbTimeLess);
        jrbTimeLess.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jrbTimeLess.setText("Less or equal to");
        jrbTimeLess.setActionCommand("<=");
        jrbTimeLess.setEnabled(false);
        jrbTimeLess.setFocusPainted(false);
        jrbTimeLess.setOpaque(false);
        jrbTimeLess.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jrbTimeLessActionPerformed(evt);
            }
        });
        jpCustomizer.add(jrbTimeLess);
        jrbTimeLess.setBounds(30, 110, 150, 20);

        jspiLessTime.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jspiLessTime.setEnabled(false);
        jpCustomizer.add(jspiLessTime);
        jspiLessTime.setBounds(50, 130, 130, 20);

        jcbRoute.setBackground(new java.awt.Color(255, 255, 255));
        jcbRoute.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jcbRoute.setText("Schedule's Route");
        jcbRoute.setFocusPainted(false);
        jcbRoute.setOpaque(false);
        jcbRoute.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jcbRouteActionPerformed(evt);
            }
        });
        jpCustomizer.add(jcbRoute);
        jcbRoute.setBounds(20, 150, 170, 20);

        jspRoute.setEnabled(false);
        jspRoute.getVerticalScrollBar().setUnitIncrement(16);

        jltRoute.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jltRoute.setEnabled(false);
        jspRoute.setViewportView(jltRoute);

        jspRoute.getVerticalScrollBar().setUI(new CustomScrollBarUI());

        jpCustomizer.add(jspRoute);
        jspRoute.setBounds(30, 170, 150, 90);

        jrbTrain.setBackground(new java.awt.Color(255, 255, 255));
        jrbTrain.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jrbTrain.setText("Train ID");
        jrbTrain.setFocusPainted(false);
        jrbTrain.setOpaque(false);
        jrbTrain.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jrbTrainActionPerformed(evt);
            }
        });
        jpCustomizer.add(jrbTrain);
        jrbTrain.setBounds(20, 260, 160, 20);

        jspTrain.setEnabled(false);

        jltTrain.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jltTrain.setEnabled(false);
        jspTrain.setViewportView(jltTrain);

        jspTrain.getVerticalScrollBar().setUI(new CustomScrollBarUI());

        jpCustomizer.add(jspTrain);
        jspTrain.setBounds(30, 280, 150, 90);

        jrbExpReport.setBackground(new java.awt.Color(255, 255, 255));
        jbgReportType.add(jrbExpReport);
        jrbExpReport.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jrbExpReport.setText("Exception Report");
        jrbExpReport.setActionCommand("exc");
        jrbExpReport.setOpaque(false);
        jrbExpReport.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jrbExpReportActionPerformed(evt);
            }
        });
        jpCustomizer.add(jrbExpReport);
        jrbExpReport.setBounds(10, 380, 180, 23);

        jrbMostPopular.setBackground(new java.awt.Color(255, 255, 255));
        jbgExceptionType.add(jrbMostPopular);
        jrbMostPopular.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jrbMostPopular.setSelected(true);
        jrbMostPopular.setText("Most popular schedule");
        jrbMostPopular.setActionCommand("mps");
        jrbMostPopular.setEnabled(false);
        jrbMostPopular.setOpaque(false);
        jrbMostPopular.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jrbMostPopularActionPerformed(evt);
            }
        });
        jpCustomizer.add(jrbMostPopular);
        jrbMostPopular.setBounds(20, 400, 160, 20);

        jlblMostTop.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jlblMostTop.setText("Top");
        jlblMostTop.setEnabled(false);
        jpCustomizer.add(jlblMostTop);
        jlblMostTop.setBounds(60, 420, 30, 20);

        jspiMostTop.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jspiMostTop.setModel(new javax.swing.SpinnerNumberModel(Integer.valueOf(0), Integer.valueOf(0), null, Integer.valueOf(1)));
        jspiMostTop.setToolTipText("0 to display all");
        jspiMostTop.setEnabled(false);
        jpCustomizer.add(jspiMostTop);
        jspiMostTop.setBounds(90, 420, 40, 20);

        jrbLessPopular.setBackground(new java.awt.Color(255, 255, 255));
        jbgExceptionType.add(jrbLessPopular);
        jrbLessPopular.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jrbLessPopular.setText("Less popular schedule");
        jrbLessPopular.setActionCommand("lps");
        jrbLessPopular.setEnabled(false);
        jrbLessPopular.setOpaque(false);
        jrbLessPopular.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jrbLessPopularActionPerformed(evt);
            }
        });
        jpCustomizer.add(jrbLessPopular);
        jrbLessPopular.setBounds(20, 440, 160, 20);

        jspiLessTop.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jspiLessTop.setModel(new javax.swing.SpinnerNumberModel(Integer.valueOf(0), Integer.valueOf(0), null, Integer.valueOf(1)));
        jspiLessTop.setToolTipText("0 to display all");
        jspiLessTop.setEnabled(false);
        jpCustomizer.add(jspiLessTop);
        jspiLessTop.setBounds(90, 460, 40, 20);

        jlblLessTop.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jlblLessTop.setText("Top");
        jlblLessTop.setEnabled(false);
        jpCustomizer.add(jlblLessTop);
        jlblLessTop.setBounds(60, 460, 30, 20);

        jrbPruchaseTimes.setBackground(new java.awt.Color(255, 255, 255));
        jbgExceptionType.add(jrbPruchaseTimes);
        jrbPruchaseTimes.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jrbPruchaseTimes.setText("Schedule purchase times");
        jrbPruchaseTimes.setActionCommand("spt");
        jrbPruchaseTimes.setEnabled(false);
        jrbPruchaseTimes.setOpaque(false);
        jrbPruchaseTimes.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jrbPruchaseTimesActionPerformed(evt);
            }
        });
        jpCustomizer.add(jrbPruchaseTimes);
        jrbPruchaseTimes.setBounds(20, 480, 170, 20);

        jrbTimesMoreThan.setBackground(new java.awt.Color(255, 255, 255));
        jbgPurchaseTime.add(jrbTimesMoreThan);
        jrbTimesMoreThan.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jrbTimesMoreThan.setSelected(true);
        jrbTimesMoreThan.setText("More than");
        jrbTimesMoreThan.setActionCommand("m");
        jrbTimesMoreThan.setEnabled(false);
        jrbTimesMoreThan.setOpaque(false);
        jrbTimesMoreThan.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jrbTimesMoreThanActionPerformed(evt);
            }
        });
        jpCustomizer.add(jrbTimesMoreThan);
        jrbTimesMoreThan.setBounds(30, 500, 81, 20);

        jrbTimesLessThan.setBackground(new java.awt.Color(255, 255, 255));
        jbgPurchaseTime.add(jrbTimesLessThan);
        jrbTimesLessThan.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jrbTimesLessThan.setText("Less than");
        jrbTimesLessThan.setActionCommand("l");
        jrbTimesLessThan.setEnabled(false);
        jrbTimesLessThan.setOpaque(false);
        jrbTimesLessThan.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jrbTimesLessThanActionPerformed(evt);
            }
        });
        jpCustomizer.add(jrbTimesLessThan);
        jrbTimesLessThan.setBounds(30, 620, 80, 20);

        jspiTimesMoreThan.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jspiTimesMoreThan.setModel(new javax.swing.SpinnerNumberModel(Integer.valueOf(1), Integer.valueOf(1), null, Integer.valueOf(1)));
        jspiTimesMoreThan.setEnabled(false);
        jpCustomizer.add(jspiTimesMoreThan);
        jspiTimesMoreThan.setBounds(130, 500, 40, 20);

        jspiTimesLessThan.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jspiTimesLessThan.setModel(new javax.swing.SpinnerNumberModel(Integer.valueOf(1), Integer.valueOf(1), null, Integer.valueOf(1)));
        jspiTimesLessThan.setEnabled(false);
        jpCustomizer.add(jspiTimesLessThan);
        jspiTimesLessThan.setBounds(130, 620, 40, 20);

        jbgPurchaseTime.add(jrbTimesMoreEquals);
        jrbTimesMoreEquals.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jrbTimesMoreEquals.setText("More than or equals to");
        jrbTimesMoreEquals.setActionCommand("me");
        jrbTimesMoreEquals.setEnabled(false);
        jrbTimesMoreEquals.setOpaque(false);
        jrbTimesMoreEquals.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jrbTimesMoreEqualsActionPerformed(evt);
            }
        });
        jpCustomizer.add(jrbTimesMoreEquals);
        jrbTimesMoreEquals.setBounds(30, 520, 150, 20);

        jbgPurchaseTime.add(jrbTimesLessEquals);
        jrbTimesLessEquals.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jrbTimesLessEquals.setText("Less than or equals to");
        jrbTimesLessEquals.setActionCommand("le");
        jrbTimesLessEquals.setEnabled(false);
        jrbTimesLessEquals.setOpaque(false);
        jrbTimesLessEquals.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jrbTimesLessEqualsActionPerformed(evt);
            }
        });
        jpCustomizer.add(jrbTimesLessEquals);
        jrbTimesLessEquals.setBounds(30, 580, 150, 20);

        jbgPurchaseTime.add(jrbTimesEquals);
        jrbTimesEquals.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jrbTimesEquals.setText("Equals to");
        jrbTimesEquals.setActionCommand("e");
        jrbTimesEquals.setEnabled(false);
        jrbTimesEquals.setOpaque(false);
        jrbTimesEquals.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jrbTimesEqualsActionPerformed(evt);
            }
        });
        jpCustomizer.add(jrbTimesEquals);
        jrbTimesEquals.setBounds(30, 560, 80, 20);

        jspiTimesMoreEquals.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jspiTimesMoreEquals.setModel(new javax.swing.SpinnerNumberModel(Integer.valueOf(1), Integer.valueOf(1), null, Integer.valueOf(1)));
        jspiTimesMoreEquals.setEnabled(false);
        jpCustomizer.add(jspiTimesMoreEquals);
        jspiTimesMoreEquals.setBounds(50, 540, 40, 20);

        jspiTimesEquals.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jspiTimesEquals.setModel(new javax.swing.SpinnerNumberModel(Integer.valueOf(1), Integer.valueOf(1), null, Integer.valueOf(1)));
        jspiTimesEquals.setEnabled(false);
        jpCustomizer.add(jspiTimesEquals);
        jspiTimesEquals.setBounds(130, 560, 40, 22);

        jspiTimesLessEquals.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jspiTimesLessEquals.setModel(new javax.swing.SpinnerNumberModel(Integer.valueOf(1), Integer.valueOf(1), null, Integer.valueOf(1)));
        jspiTimesLessEquals.setEnabled(false);
        jpCustomizer.add(jspiTimesLessEquals);
        jspiTimesLessEquals.setBounds(50, 600, 40, 20);

        jbgPurchaseTime.add(jrbTimesNotEquals);
        jrbTimesNotEquals.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jrbTimesNotEquals.setText("Not equals to");
        jrbTimesNotEquals.setActionCommand("ne");
        jrbTimesNotEquals.setEnabled(false);
        jrbTimesNotEquals.setOpaque(false);
        jrbTimesNotEquals.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jrbTimesNotEqualsActionPerformed(evt);
            }
        });
        jpCustomizer.add(jrbTimesNotEquals);
        jrbTimesNotEquals.setBounds(30, 640, 99, 20);

        jspiTimesNotEquals.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jspiTimesNotEquals.setModel(new javax.swing.SpinnerNumberModel(Integer.valueOf(1), Integer.valueOf(1), null, Integer.valueOf(1)));
        jspiTimesNotEquals.setEnabled(false);
        jpCustomizer.add(jspiTimesNotEquals);
        jspiTimesNotEquals.setBounds(130, 640, 40, 20);

        jspCustomizer.setViewportView(jpCustomizer);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jspCustomizer, javax.swing.GroupLayout.DEFAULT_SIZE, 210, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jspCustomizer, javax.swing.GroupLayout.DEFAULT_SIZE, 340, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

    private void depTimeHaving() {
        String operator = jbgTimeGroup.getSelection().getActionCommand();
        switch (operator) {
            case ">=":
                qb.having("departureTime", format.format((Date) jspiGreaterTime.getValue()), QueryBuilder.MORE_THAN_EQUALS);
                break;
            case "<=":
                qb.having("departureTime", format.format((Date) jspiLessTime.getValue()), QueryBuilder.LESS_THAN_EQUALS);
                break;
        }
    }

    private void depTimeAnd() {
        String operator = jbgTimeGroup.getSelection().getActionCommand();
        switch (operator) {
            case ">=":
                qb.and("departureTime", format.format((Date) jspiGreaterTime.getValue()), QueryBuilder.MORE_THAN_EQUALS);
                break;
            case "<=":
                qb.and("departureTime", format.format((Date) jspiLessTime.getValue()), QueryBuilder.MORE_THAN_EQUALS);
                break;
        }
    }

    private void routeHaving() {
        List<String> routeList = jltRoute.getSelectedValuesList();
        qb.having("routeName", routeList.get(0));
        for (int i = 1; i < routeList.size(); i++) {
            qb.or("routeName", routeList.get(i));
        }
    }

    private void routeAnd() {
        List<String> routeList = jltRoute.getSelectedValuesList();
        if (!routeList.isEmpty()) {
            qb.and("routeName", routeList.get(0));
            for (int i = 1; i < routeList.size(); i++) {
                qb.or("routeName", routeList.get(i));
            }
        }
    }

    private void trainHaving() {
        List<String> routeList = jltTrain.getSelectedValuesList();
        qb.having("trainName", routeList.get(0));
        for (int i = 1; i < routeList.size(); i++) {
            qb.or("trainName", routeList.get(i));
        }
    }

    private void trainAnd() {
        List<String> routeList = jltTrain.getSelectedValuesList();
        if (!routeList.isEmpty()) {
            qb.and("trainName", routeList.get(0));
            for (int i = 1; i < routeList.size(); i++) {
                qb.or("trainName", routeList.get(i));
            }
        }
    }

    private void jrbSumReportActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jrbSumReportActionPerformed
        jcbDepTime.setEnabled(true);
        jcbRoute.setEnabled(true);
        jrbTrain.setEnabled(true);
        jrbMostPopular.setEnabled(false);
        jlblMostTop.setEnabled(false);
        jspiMostTop.setEnabled(false);
        jrbLessPopular.setEnabled(false);
        jrbPruchaseTimes.setEnabled(false);
        jlblLessTop.setEnabled(false);
        jspiLessTop.setEnabled(false);
        jrbTimesMoreThan.setEnabled(false);
        jrbTimesLessThan.setEnabled(false);
        jspiTimesMoreThan.setEnabled(false);
        jspiTimesLessThan.setEnabled(false);
    }//GEN-LAST:event_jrbSumReportActionPerformed

    private void jcbDepTimeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jcbDepTimeActionPerformed
        if (jcbDepTime.isSelected()) {
            depTime = true;
            jrbTimeGreater.setEnabled(true);
            jspiGreaterTime.setEnabled(true);
            jrbTimeLess.setEnabled(true);
        } else {
            depTime = false;
            jrbTimeGreater.setEnabled(false);
            jspiGreaterTime.setEnabled(false);
            jrbTimeLess.setEnabled(false);
            jspiLessTime.setEnabled(false);
        }
    }//GEN-LAST:event_jcbDepTimeActionPerformed

    private void jrbTimeGreaterActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jrbTimeGreaterActionPerformed
        if (jrbTimeGreater.isSelected()) {
            jspiGreaterTime.setEnabled(true);
            jspiLessTime.setEnabled(false);
        }
    }//GEN-LAST:event_jrbTimeGreaterActionPerformed

    private void jrbTimeLessActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jrbTimeLessActionPerformed
        if (jrbTimeLess.isSelected()) {
            jspiLessTime.setEnabled(true);
            jspiGreaterTime.setEnabled(false);
        }
    }//GEN-LAST:event_jrbTimeLessActionPerformed

    private void jcbRouteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jcbRouteActionPerformed
        if (jcbRoute.isSelected()) {
            route = true;
            jltRoute.setSelectedIndex(0);
            jltRoute.setEnabled(true);
            jspRoute.setEnabled(true);
        } else {
            route = false;
            jspRoute.setEnabled(false);
            jltRoute.clearSelection();
            jltRoute.setEnabled(false);
        }
    }//GEN-LAST:event_jcbRouteActionPerformed

    private void jrbTrainActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jrbTrainActionPerformed
        if (jrbTrain.isSelected()) {
            train = true;
            jspTrain.setEnabled(true);
            jltTrain.setSelectedIndex(0);
            jltTrain.setEnabled(true);
        } else {
            train = false;
            jspTrain.setEnabled(false);
            jltTrain.clearSelection();
            jltTrain.setEnabled(false);
        }
    }//GEN-LAST:event_jrbTrainActionPerformed

    private void jrbExpReportActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jrbExpReportActionPerformed
        jcbDepTime.setEnabled(false);
        jcbRoute.setEnabled(false);
        jrbTrain.setEnabled(false);
        jltRoute.setEnabled(false);
        jltTrain.setEnabled(false);
        jrbTimeGreater.setEnabled(false);
        jrbTimeLess.setEnabled(false);
        jspiGreaterTime.setEnabled(false);
        jspiLessTime.setEnabled(false);
        jrbMostPopular.setEnabled(true);
        jlblMostTop.setEnabled(true);
        jspiMostTop.setEnabled(true);
        jrbLessPopular.setEnabled(true);
        jrbPruchaseTimes.setEnabled(true);
    }//GEN-LAST:event_jrbExpReportActionPerformed

    private void jrbMostPopularActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jrbMostPopularActionPerformed
        jlblMostTop.setEnabled(true);
        jspiMostTop.setEnabled(true);
        jlblLessTop.setEnabled(false);
        jspiLessTop.setEnabled(false);
        jrbTimesMoreThan.setEnabled(false);
        jspiTimesMoreThan.setEnabled(false);
        jrbTimesLessThan.setEnabled(false);
        jspiTimesLessThan.setEnabled(false);
        jrbTimesEquals.setEnabled(false);
        jspiTimesEquals.setEnabled(false);
        jrbTimesLessEquals.setEnabled(false);
        jspiTimesLessEquals.setEnabled(false);
        jrbTimesMoreEquals.setEnabled(false);
        jspiTimesMoreEquals.setEnabled(false);
        jrbTimesNotEquals.setEnabled(false);
        jspiTimesNotEquals.setEnabled(false);
    }//GEN-LAST:event_jrbMostPopularActionPerformed

    private void jrbLessPopularActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jrbLessPopularActionPerformed
        jlblMostTop.setEnabled(false);
        jspiMostTop.setEnabled(false);
        jlblLessTop.setEnabled(true);
        jspiLessTop.setEnabled(true);
        jrbTimesMoreThan.setEnabled(false);
        jspiTimesMoreThan.setEnabled(false);
        jrbTimesLessThan.setEnabled(false);
        jspiTimesLessThan.setEnabled(false);
        jrbTimesEquals.setEnabled(false);
        jspiTimesEquals.setEnabled(false);
        jrbTimesLessEquals.setEnabled(false);
        jspiTimesLessEquals.setEnabled(false);
        jrbTimesMoreEquals.setEnabled(false);
        jspiTimesMoreEquals.setEnabled(false);
        jrbTimesNotEquals.setEnabled(false);
        jspiTimesNotEquals.setEnabled(false);
    }//GEN-LAST:event_jrbLessPopularActionPerformed

    private void jrbPruchaseTimesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jrbPruchaseTimesActionPerformed
        jlblMostTop.setEnabled(false);
        jspiMostTop.setEnabled(false);
        jlblLessTop.setEnabled(false);
        jspiLessTop.setEnabled(false);
        jrbTimesMoreThan.setEnabled(true);
        jspiTimesMoreThan.setEnabled(true);
        jrbTimesLessThan.setEnabled(true);
        jspiTimesLessThan.setEnabled(false);
        jrbTimesEquals.setEnabled(true);
        jspiTimesEquals.setEnabled(false);
        jrbTimesLessEquals.setEnabled(true);
        jspiTimesLessEquals.setEnabled(false);
        jrbTimesMoreEquals.setEnabled(true);
        jspiTimesMoreEquals.setEnabled(false);
        jrbTimesNotEquals.setEnabled(true);
        jspiTimesNotEquals.setEnabled(false);
    }//GEN-LAST:event_jrbPruchaseTimesActionPerformed

    private void jrbTimesMoreThanActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jrbTimesMoreThanActionPerformed
        jspiTimesMoreThan.setEnabled(true);
        jspiTimesLessThan.setEnabled(false);
        jspiTimesEquals.setEnabled(false);
        jspiTimesLessEquals.setEnabled(false);
        jspiTimesMoreEquals.setEnabled(false);
        jspiTimesNotEquals.setEnabled(false);
    }//GEN-LAST:event_jrbTimesMoreThanActionPerformed

    private void jrbTimesLessThanActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jrbTimesLessThanActionPerformed
        jspiTimesMoreThan.setEnabled(false);
        jspiTimesLessThan.setEnabled(true);
        jspiTimesEquals.setEnabled(false);
        jspiTimesLessEquals.setEnabled(false);
        jspiTimesMoreEquals.setEnabled(false);
        jspiTimesNotEquals.setEnabled(false);
    }//GEN-LAST:event_jrbTimesLessThanActionPerformed

    private void jrbTimesMoreEqualsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jrbTimesMoreEqualsActionPerformed
        jspiTimesMoreThan.setEnabled(false);
        jspiTimesLessThan.setEnabled(false);
        jspiTimesEquals.setEnabled(false);
        jspiTimesLessEquals.setEnabled(false);
        jspiTimesMoreEquals.setEnabled(true);
        jspiTimesNotEquals.setEnabled(false);
    }//GEN-LAST:event_jrbTimesMoreEqualsActionPerformed

    private void jrbTimesEqualsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jrbTimesEqualsActionPerformed
        jspiTimesMoreThan.setEnabled(false);
        jspiTimesLessThan.setEnabled(false);
        jspiTimesEquals.setEnabled(true);
        jspiTimesLessEquals.setEnabled(false);
        jspiTimesMoreEquals.setEnabled(false);
        jspiTimesNotEquals.setEnabled(false);
    }//GEN-LAST:event_jrbTimesEqualsActionPerformed

    private void jrbTimesLessEqualsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jrbTimesLessEqualsActionPerformed
        jspiTimesMoreThan.setEnabled(false);
        jspiTimesLessThan.setEnabled(false);
        jspiTimesEquals.setEnabled(false);
        jspiTimesLessEquals.setEnabled(true);
        jspiTimesMoreEquals.setEnabled(false);
        jspiTimesNotEquals.setEnabled(false);
    }//GEN-LAST:event_jrbTimesLessEqualsActionPerformed

    private void jrbTimesNotEqualsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jrbTimesNotEqualsActionPerformed
        jspiTimesMoreThan.setEnabled(false);
        jspiTimesLessThan.setEnabled(false);
        jspiTimesEquals.setEnabled(false);
        jspiTimesLessEquals.setEnabled(false);
        jspiTimesMoreEquals.setEnabled(false);
        jspiTimesNotEquals.setEnabled(true);
    }//GEN-LAST:event_jrbTimesNotEqualsActionPerformed

    public String buildQuery() {
        qb = new QueryBuilder();
        String type = jbgReportType.getSelection().getActionCommand();
        initialHave = 0;
        if (type.equals("sum")) {
            qb.select("scheduleName", "departureTime", "s.status", "routeName", "trainName").from("schedule s", "route r", "train t");
            qb.where("s.rid", "r.rid", false).and("s.trainId", "t.trainId", false);
            if (depTime) {
                if (initialHave != 2 && initialHave != 3) {
                    initialHave = 1;
                    depTimeHaving();
                } else {
                    depTimeAnd();
                }
            }
            if (route) {
                if (initialHave != 1 && initialHave != 3) {
                    initialHave = 2;
                    routeHaving();
                } else {
                    routeAnd();
                }
            }
            if (train) {
                if (initialHave != 2 && initialHave != 1) {
                    initialHave = 3;
                    trainHaving();
                } else {
                    trainAnd();
                }
            }
        } else {
            String except = jbgExceptionType.getSelection().getActionCommand();
            int number;
            qb.select("DATE_FORMAT(datePurchased, \"%m-%Y\") AS month", "scheduleName", "origin", "destination", "COUNT(scheduleName) AS times");
            qb.from("ticket t", "schedule s").where("t.shid", "s.shid", false).groupBy("month", "scheduleName", "origin", "destination");
            switch (except) {
                case "mps":
                    number = (int) jspiMostTop.getValue();
                    qb.orderBy("times", QueryBuilder.DESC, true);
                    if (number != 0) {
                        qb.limit(number);
                    }
                    break;
                case "lps":
                    number = (int) jspiLessTop.getValue();
                    qb.orderBy("times", QueryBuilder.ASC, true);
                    if (number != 0) {
                        qb.limit(number);
                    }
                    break;
                case "spt":
                    String operator = jbgPurchaseTime.getSelection().getActionCommand();
                    switch (operator) {
                        case "m":
                            number = (int) jspiTimesMoreThan.getValue();
                            qb.having("times", number, QueryBuilder.MORE_THAN);
                            break;
                        case "me":
                            number = (int) jspiTimesMoreEquals.getValue();
                            qb.having("times", number, QueryBuilder.MORE_THAN_EQUALS);
                            break;
                        case "e":
                            number = (int) jspiTimesEquals.getValue();
                            qb.having("times", number);
                            break;
                        case "le":
                            number = (int) jspiTimesLessEquals.getValue();
                            qb.having("times", number, QueryBuilder.LESS_THAN_EQUALS);
                            break;
                        case "l":
                            number = (int) jspiTimesLessThan.getValue();
                            qb.having("times", number, QueryBuilder.LESS_THAN);
                            break;
                        case "ne":
                            number = (int) jspiTimesNotEquals.getValue();
                            qb.having("times", number, QueryBuilder.NOT_EQUALS);
                            break;
                    }
                    qb.orderBy("times", QueryBuilder.DESC, true);
                    break;
            }
        }
        return qb.build();
    }

    public boolean isSummaryReport() {
        return jrbSumReport.isSelected();
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup jbgExceptionType;
    private javax.swing.ButtonGroup jbgPurchaseTime;
    private javax.swing.ButtonGroup jbgReportType;
    private javax.swing.ButtonGroup jbgTimeGroup;
    private javax.swing.JCheckBox jcbDepTime;
    private javax.swing.JCheckBox jcbRoute;
    private javax.swing.JLabel jlblLessTop;
    private javax.swing.JLabel jlblMostTop;
    private javax.swing.JLabel jlblScheduleCustomizer;
    private javax.swing.JList jltRoute;
    private javax.swing.JList jltTrain;
    private javax.swing.JPanel jpCustomizer;
    private javax.swing.JRadioButton jrbExpReport;
    private javax.swing.JRadioButton jrbLessPopular;
    private javax.swing.JRadioButton jrbMostPopular;
    private javax.swing.JRadioButton jrbPruchaseTimes;
    private javax.swing.JRadioButton jrbSumReport;
    private javax.swing.JRadioButton jrbTimeGreater;
    private javax.swing.JRadioButton jrbTimeLess;
    private javax.swing.JRadioButton jrbTimesEquals;
    private javax.swing.JRadioButton jrbTimesLessEquals;
    private javax.swing.JRadioButton jrbTimesLessThan;
    private javax.swing.JRadioButton jrbTimesMoreEquals;
    private javax.swing.JRadioButton jrbTimesMoreThan;
    private javax.swing.JRadioButton jrbTimesNotEquals;
    private javax.swing.JCheckBox jrbTrain;
    private javax.swing.JScrollPane jspCustomizer;
    private javax.swing.JScrollPane jspRoute;
    private javax.swing.JScrollPane jspTrain;
    private javax.swing.JSpinner jspiGreaterTime;
    private javax.swing.JSpinner jspiLessTime;
    private javax.swing.JSpinner jspiLessTop;
    private javax.swing.JSpinner jspiMostTop;
    private javax.swing.JSpinner jspiTimesEquals;
    private javax.swing.JSpinner jspiTimesLessEquals;
    private javax.swing.JSpinner jspiTimesLessThan;
    private javax.swing.JSpinner jspiTimesMoreEquals;
    private javax.swing.JSpinner jspiTimesMoreThan;
    private javax.swing.JSpinner jspiTimesNotEquals;
    private javax.swing.JSeparator jsprCustomizer;
    // End of variables declaration//GEN-END:variables
}
